package com.saitron.zhky.provider.health.web;

import java.io.File;
import java.util.*;

public class Updater {
    public static void main(String[] args) {
        String projectPath="D:\\workspace\\idea\\RuoYi-Cloud-Plus-2.X";
        updateNames(new File(projectPath));
    }

    //需要改名的目录名称（包含即可）
    static Map<String,String> reNameDirMap=new LinkedHashMap<>();
    static {
        reNameDirMap.put("ruoyi","zyym");
        reNameDirMap.put("RuoYi","Zyym");
        reNameDirMap.put("RUOYI","ZYYM");
        reNameDirMap.put("ruoYi","zyym");
        reNameDirMap.put("dromara","zyym");
        reNameDirMap.put("Dromara","Zyym");
        reNameDirMap.put("DROMARA","ZYYM");
    }

    //需要改名的父级目录名称（org.dromara这种，改org为com）
    static Map<String,String> reNameDirMapByParent=new LinkedHashMap<>();
    static {
        reNameDirMapByParent.put("org","com");
    }

    //需要改名的文件名称（包含即可）
    static Map<String,String> reNameFileMap=new LinkedHashMap<>();
    static {
        reNameFileMap.put("ruoyi","zyym");
        reNameFileMap.put("RuoYi","Zyym");
        reNameFileMap.put("RUOYI","ZYYM");
        reNameFileMap.put("ruoYi","zyym");
        reNameFileMap.put("dromara","zyym");
        reNameFileMap.put("Dromara","Zyym");
        reNameFileMap.put("DROMARA","ZYYM");
        reNameFileMap.put("ry-","zyym-");
        reNameFileMap.put("_ry_","_zyym_");
    }

    //哪些文件后缀需要修改内容
    static List<String> reContentExtList=new ArrayList<>();
    static {
        reContentExtList.add(".java");
        reContentExtList.add(".vue");
        reContentExtList.add(".js");
        reContentExtList.add(".ts");
        reContentExtList.add(".json");
        reContentExtList.add(".html");
        reContentExtList.add(".css");
        reContentExtList.add(".scss");
        reContentExtList.add(".yml");
        reContentExtList.add(".yaml");
        reContentExtList.add(".sql");
        reContentExtList.add(".xml");
        reContentExtList.add(".LoadBalance");
        reContentExtList.add(".vm");
        reContentExtList.add(".factories");
        reContentExtList.add(".imports");
        reContentExtList.add(".Filter");
        reContentExtList.add(".properties");
        reContentExtList.add("Dockerfile");
        reContentExtList.add(".md");
    }

    //需要改内容的关键词
    static Map<String,String> reContentKeyMap=new LinkedHashMap<>();
    static {
        reContentKeyMap.put("org.dromara","com.zyym");
        reContentKeyMap.put("ruoyi","zyym");
        reContentKeyMap.put("RuoYi","Zyym");
        reContentKeyMap.put("RUOYI","ZYYM");
        reContentKeyMap.put("ruoYi","zyym");
        reContentKeyMap.put("dromara","zyym");
        reContentKeyMap.put("Dromara","Zyym");
        reContentKeyMap.put("DROMARA","ZYYM");
        reContentKeyMap.put("com.zyym.sms4j","org.dromara.sms4j");//还原不需要替换的sms4j
        reContentKeyMap.put("com.zyym.easy","org.dromara.easy");//还原不需要替换的easy
    }
    //需要改内容的关键词（sql专用）
    static Map<String,String> reSqlKeyMap=new LinkedHashMap<>();
    static {
        reSqlKeyMap.put("ry-","zyym-");
    }
    //需要改内容的关键词（yml和properties专用）
    static Map<String,String> reYmlKeyMap=new LinkedHashMap<>();
    static {
        reYmlKeyMap.put("/ry-","/zyym-");
    }

    //改名
    public static void updateNames(File file) {
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            boolean exist=false;
            for (int i = 0; i < listFiles.length; i++) {
                if(reNameDirMap.containsKey(listFiles[i].getName())&&listFiles[i].isDirectory()){
                    exist=true;
                }
                updateNames(listFiles[i]);
            }
            if(exist&&reNameDirMapByParent.containsKey(file.getName())){
                //修改名称org为com
                file.renameTo(new File(file.getParentFile().getAbsolutePath()+File.separator+reNameDirMapByParent.get(file.getName())));
            }else{
                //修改名称ruoyi-xxx为zyym-xxx等各种包含的
                Set<String> keys = reNameDirMap.keySet();
                for (String key:keys) {
                    if(file.getName().contains(key)){
                        String newName = file.getName().replace(key, reNameDirMap.get(key));
                        File newFile=new File(file.getParentFile().getAbsolutePath()+File.separator+newName);
                        file.renameTo(newFile);
                        if(!newFile.exists()){
                            FileUtil.moveFile(newFile,file);
                        }
                        break;
                    }
                }
            }
        }else if(file.isFile()){
            //修改名称ruoyi-xxx为zyym-xxx等各种包含的
            Set<String> keys = reNameFileMap.keySet();
            for (String key:keys) {
                if(file.getName().contains(key)){
                    String newName = file.getName().replace(key, reNameFileMap.get(key));
                    File newFile=new File(file.getParentFile().getAbsolutePath()+File.separator+newName);
                    file.renameTo(newFile);
                    if(!newFile.exists()){
                        FileUtil.moveFile(newFile,file);
                    }
                    file=newFile;
                    break;
                }
            }
            updateContent(file);
        }
    }

    //改内容
    public static void updateContent(File file) {
        //判断是否需要修改内容
        String suffiex = FileUtil.getSuffiex(file.getName());
        for (String s :reContentExtList) {
            if(file.getName().endsWith(s)){
                String content = FileUtil.readTextFile(file);
                for (String k:reContentKeyMap.keySet()) {
                    if(content.contains(k)){
                        content=content.replaceAll(k,reContentKeyMap.get(k));
                    }
                }
                if(".sql".equals(suffiex)){
                    for (String k:reSqlKeyMap.keySet()) {
                        if(content.contains(k)){
                            content=content.replaceAll(k,reSqlKeyMap.get(k));
                        }
                    }
                }

                if(".yml".equals(suffiex)||".properties".equals(suffiex)){
                    for (String k:reYmlKeyMap.keySet()) {
                        if(content.contains(k)){
                            content=content.replaceAll(k,reYmlKeyMap.get(k));
                        }
                    }
                }
                //替换内容并保存
                FileUtil.saveTextFile(file,content);
                break;
            }
        }
    }
}
